package com.moshang.blog.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moshang.blog.core.utils.MyException;
import com.moshang.blog.entity.SysRole;
import com.moshang.blog.entity.SysRoleMenu;
import com.moshang.blog.core.constant.MySysUser;
import com.moshang.blog.mapper.SysRoleMapper;


import com.moshang.blog.service.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.moshang.blog.service.SysRoleService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Override
    public List<SysRole> selectRoleByUserId(Integer id) {
        return baseMapper.selectRoleByUserId(id);
    }

    @Override
    public int getRoleNameCount(String roleName) {
        QueryWrapper<SysRole> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("role_name",roleName);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public void updateRole(SysRole role,Integer[] menus) {
     //更新角色信息
        baseMapper.updateById(role);
        //删除中间信息
        baseMapper.deleteRoleMenus(role.getId());
        SysRoleMenu sysRoleMenu=new SysRoleMenu();
        sysRoleMenu.setRoleId(role.getId());
        if (menus != null) {
            for (Integer menu : menus) {
                sysRoleMenu.setMenuId(menu);
                sysRoleMenuService.save(sysRoleMenu);
            }
        }
    }

    @Override
    public void saveRole(SysRole role,Integer[] menus) {

        role.setCreateBy(MySysUser.currentUser().getId());
        role.setUpdateBy(MySysUser.currentUser().getId());
        role.setCreateTime(new Date());
        role.setUpdateTime(new Date());
        role.setCreateName(MySysUser.userName());
        role.setUpdateName(MySysUser.userName());
        role.setIsActivited(0);
        baseMapper.insert(role);
        SysRoleMenu sysRoleMenu=new SysRoleMenu();
        sysRoleMenu.setRoleId(role.getId());
        if (menus != null) {
            for (Integer menu : menus) {
                sysRoleMenu.setMenuId(menu);
                sysRoleMenuService.save(sysRoleMenu);
            }
        }
    }

    @Override
    public void deleteRole(SysRole role) {
        baseMapper.deleteById(role.getId());
        baseMapper.deleteRoleMenus(role.getId());
        baseMapper.deleteRoleUsers(role.getId());
    }

    @Override
    public List<SysRole> selectAll() {
        QueryWrapper<SysRole> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("is_activited",0);
        List<SysRole> roleList = baseMapper.selectList(queryWrapper);
        return roleList;
    }

    @Override
    public String showAll(SysRole role) {
        List<SysRole> tList = null;
        try {
            tList = sysRoleMapper.selectList(null);
        } catch (MyException e) {
            e.printStackTrace();
        }
        return JSON.toJSONString(tList);
    }
}
